// pages/personalCenter/rechargeAmount/rechargeAmount.js
import { paymentCreateOrder } from '../../../../common/api/my';
import {
  commodityList
} from '../../../../common/api/shop.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    priceError: true,
    price: 0,
    commodityArr: [],
    page: 1, // 页码
    bottomLoading: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getShop();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(!this.data.bottomLoading){
      console.log('已经没有了')
      return
    }
    this.data.page+=1;
    this.getShop()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onPriceInput(e) {
    const {
      priceError
    } = this.data;
    console.log(e.detail.value)
    const isNumber = /^\d+(\.\d+)?$/.test(e.detail.value);
    if (priceError === isNumber) {
      this.setData({
        priceError: !isNumber
      });
    };
    this.data.price = e.detail.value;
  },
  // 
  async submit() {
    console.log(this.data.price)
    if(this.data.priceError){
      wx.showToast({
        title: '请输入正确的充值金额',
        icon: 'none',
       
      });
      return
    }else if(this.data.price < 0.01){
      wx.showToast({
        title: '最低起充0.01元',
        icon: 'none'
      });
      return
    };
    wx.showLoading({
      title: '请稍等正在生成订单中',
      mask: true
    });
    let user = wx.getStorageSync('userInfo');
    let data = {
      price: this.data.price,
      userId: user.id
    };
    let res = await paymentCreateOrder(data);
    wx.hideLoading();
    if(res){
      this.copy(`https://yuleren.cn/api/payment/pay?orderNo=${res}`)
    }else {
      wx.showToast({
        title: '订单编号不存在',
        icon: 'none'
      });
    }
    
  },
  //  复制链接
  copy(text) {
    wx.showModal({
      title: '复制链接，浏览器打开',
      content: '',
      showCancel: false,
      confirmText: '复制',
      complete: (res) => {
        if (res.confirm) {
          wx.setClipboardData({
            data: text,
            success: () => {
              wx.showToast({
                title: '复制成功',
                icon: 'none'
              })
            }
          })
        }


      }
    })
  },
   /**
   * 获取商品列表
   */
  getShop: async function () {
    let data = {
      // categoryId : this.data.tabData[this.data.activeIndex].id,
      categoryId: '',
      type: '',
      page: this.data.page,
      size: 10,
      goodsName: '',
      isOnSale: '1',
      isBuy: '0',
      isGold: 1
    };
    let res = await commodityList(data);
    // console.log(res.list, '商品列表')
    // 测试分页
    // let arr2 = [];
    // for(let i=0;i<10;i++){
    //   arr2.push(...res.list)
    // };
    // let arr = arr2 || [];
    let arr = res.list || [];
      if(arr.length < data.size){
        this.setData({
          bottomLoading: false
        })
      }
    let a = [...this.data.commodityArr, ...arr];
    this.setData({
      commodityArr: a,
      refresherTriggered: false
    });
  },
  go: function (e) {
    wx.navigateTo({
      url: '/pages/allShop/shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id + '&type=2',
      // url: '../shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id,
    })
  },
  // 购买
  buy(e){
    // console.log('点击购买')
    let id = e.currentTarget.dataset.item.id;
    let idObj = JSON.stringify([{
      id: id,
      num: 1
    }]);
    wx.navigateTo({
      url: "/pages/allShop/submitorder2/submitorder2?id=" + idObj,
    })
  }
})